<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2019/5/30
 * Time: 15:46
 */

namespace app\index\controller;


use app\index\controller\phpcode;
use app\index\model\doBuy;
use app\index\model\doCate;
use app\index\model\doGoods;
use app\index\model\doOrder;
use app\index\model\doUser;
use think\Db;
use think\facade\Cookie;
use think\facade\Request;

class Order extends Base
{
    public function initialize()
    {
        parent::initialize();
    }




    // 订单列表
    public function orderList()
    {
        $order_number = input('order_number');
        if (empty($order_number)){
            $o = [];
        }else{
            $o['order_number'] = $order_number;
        }
        $user_name = input('user_name');
        if (empty($user_name)){
            $u = [];
        }else{
            $u[] = ['user_name','like','%'.$user_name.'%'];
        }

        $orders = doOrder::where($o)
            ->where($u)
            ->paginate(10,false,['query'=>request()->param()])->each(function ($item){
            $user = doUser::get($item['uid'])->toArray();
            $item['uname'] = $user['name'];
            $item['sid'] = $user['id'];
            $item['cate'] = sizeof(json_decode($item['goods']));
            return $item;
        });

        $this->assign('page',$orders->render());
        $this->assign('orders',$orders);
        return $this->fetch();
    }
    

    // 出库详情单
    public function order()
    {
        $id = input('id');
        $orders = doOrder::where('id',$id)->find();
        $user = doUser::get($orders['uid']);
        $g = json_decode($orders['goods']);
        for($i=0;$i<sizeof($g);$i++){
            $goods[$i] = doGoods::get($g[$i]->gid)->toArray();
            $goods[$i]['num'] = $g[$i]->num;
        }
        $n = sizeof($goods);
        $title = '天津鹤实联科贸有限责任公司南开第四分公司';
        $this->assign('title',$title);
        $this->assign('user',$user);
        $this->assign('goods',$goods);
        $this->assign('n',$n);
        $this->assign('orders',$orders);
        return $this->fetch();
    }

    //小票
    public function xporder()
    {
        $id = input('id');
        $orders = doOrder::where('id',$id)->find();
        $user = doUser::get($orders['uid']);
        $g = json_decode($orders['goods']);
        for($i=0;$i<sizeof($g);$i++){
            $goods[$i] = doGoods::get($g[$i]->gid)->toArray();
            $goods[$i]['num'] = $g[$i]->num;
        }

        $title = '天津鹤实联科贸有限责任公司南开第四分公司';
        $this->assign('title',$title);
        $this->assign('user',$user);
        $this->assign('goods',$goods);
        $this->assign('orders',$orders);

        return $this->fetch();
    }

    
    // 编辑订单状态
    public function editOrder()
    {
        $id = input('id');
        if (Request::isPost()){
            $data = input('post.');

            if (doOrder::update($data)){
                return json(['code'=>1,'msg'=>'操作成功']);
            }
            return json(['code'=>0,'msg'=>'操作失败']);
        }
        $order = doOrder::where('id',$id)->find();
        $this->assign('order',$order);
        return $this->fetch();
    }

//    public function orderDel()
//    {
//        $id = input('id');
//        if (doOrder::destroy($id)){
//            return json(['code'=>1]);
//        }
//        return json(['code'=>0]);
//    }

    // 添加订单
    public function addOrder()
    {

        // 选择客户
        if (input('o') == 1 ){
            $name = input('name');
            if (empty($name)){
                $where = [];
            }else{
                $where[] = ['name','like','%'.$name.'%'];
            }
            $user = doUser::where($where)->where('status',1)->paginate(10,false,['query' => request()->param()]);
            $this->assign('page',$user->render());
            $this->assign('user',$user);
            return $this->fetch('check_User');
        }

        // 添加订单
        if (input('o') == 2){
            $uid = input('uid');
            if (Request::isPost()){
                $post = input('post.');

                foreach ($post['id'] as $k){
                    $c = Db::name('cart')->where('id',$k)->field('gid,num')->find();
                    $g = Db::name('goods')->where('id',$c['gid'])->find();
                    // 下单前判断库存
                   if ($c['num'] > $g['stock']){
                       return json(['code'=>0,'msg'=>'商品:'.$g['name'].'库存不足,剩余'.$g['stock']]);
                   }else{
                       Db::name('goods')->where('id',$c['gid'])->setDec('stock',$c['num']);
                   }
                    $goods[] = Db::name('cart')->where('id',$k)->field('gid,num')->find();
                    Db::name('cart')->where('id',$k)->delete();
                }

                $goods = json_encode($goods);
                $data['uid'] = $post['data']['uid'];
                $data['user_name'] =  $post['data']['name'];
                $data['goods'] = $goods;
                $data['total_price'] = $post['data']['total_price'];
                $data['remark'] = $post['data']['remark'];
                $data['time'] = date('Y-m-d H:i:s',time());

                $data['order_number'] = date('YmdHis',time());
                $order = new doOrder($data);
                if ($order->save()){
                    return json(['code'=>1,'msg'=>'添加成功']);
                }else{
                    return json(['code'=>0,'msg'=>'网络开小差了,添加失败']);
                }


            }
            $user = doUser::get($uid);
            $goods = Db::name('cart')->alias('c')->where('uid',$uid)->select();
            if (!empty($goods)){
                foreach($goods as $k){
                    $g[] = Db::name('goods')->where('id',$k['gid'])->field('specs,unit')->find();
                }
                for($i=0;$i<sizeof($g);$i++){
                    $goods[$i]['specs'] = $g[$i]['specs'];
                    $goods[$i]['unit'] = $g[$i]['unit'];
                }
            }

            $this->assign('user',$user);
            $this->assign('goods',$goods);
            return $this->fetch();

        }

        // 选择商品
        if (input('o' == 3)){
            $name = input('name');
            $uid = input('uid');
            if (empty($name)){
                $where = [];
            }else{
                $where[] = ['name','like','%'.$name.'%'];
            }
            $goods = doGoods::where($where)

                ->where('status',1)
                ->paginate(10,false,['query' => request()->param()])->each(function ($item){
                    $cate = doCate::get($item['cid']);
                    $item['cate']=$cate['title'];
                    return $item;
                });
            $this->assign('page',$goods->render());
            $this->assign('goods',$goods);
            $this->assign('uid',$uid);
            return $this->fetch('check_Goods');
        }

    }

    // 添加商品
    public function adCar()
    {
        $post = input('post.');
        if (empty($post['id'])){
            return json(['code'=>0]);
        }

        foreach($post['id'] as $k ){
            $e = Db::name('cart')->where('uid',$post['uid'])->where('gid',$k)->find();
            if ($e){
                Db::name('cart')->where('uid',$post['uid'])->where('gid',$k)->setInc('num',1);
            }else{
                $goods = Db::name('goods')->where('id',$k)->find();
                Db::name('cart')->insert(['gid'=>$k,'name'=>$goods['name'],'price'=>$goods['price'],'uid'=>$post['uid']]);
            }

        }

      return json(['code'=>1]);
    }

    // 改变数量
    function numChange(){
        $id = input('id');
        $num = input('num');
        Db::name('cart')->where('id',$id)->update(['num'=>$num]);
    }

    //  删除购物车商品
    function delCart(){
        $id = input('id');
        $res = Db::name('cart')->where('id',$id)->delete();
        if ($res){
            return json(['code'=>1]);
        }
    }







}